#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<stdint.h>

#pragma pack(1)
typedef struct {
    union {
		uint16_t type;
		//unsigned char type_b[2];
	};
    uint32_t size;
    uint16_t rsvd1;
    uint16_t rsvd2;
    uint32_t offbits;
} bmp_file_header;
#pragma pack()

#pragma pack(1)
typedef struct {
	uint32_t size;
	int32_t width;
	int32_t height;
	uint16_t planes;
	uint16_t bpp;
	uint32_t compression;
	uint32_t size_image;
	int32_t xppm; //x pels per meter
	int32_t yppm;
	uint32_t color_used;
	uint32_t color_important;
} bmp_info_header;
#pragma pack()

FILE *fbmp=NULL;
bmp_file_header bmp_h;
bmp_info_header bmp_i;
int32_t rgba[256];

FILE *fout=NULL;

int main(int argc, char** argv) {
	if(3<=argc) {
		if(NULL!=argv[1] && 0!=strcmp(argv[1],"")) {
		
		} else {
			printf("input file error\n");
		}
		if(NULL!=argv[2] && 0!=strcmp(argv[2],"")) {
		
		} else {
			printf("output file error\n");
		}
		fbmp=fopen(argv[1],"rb");
		if(NULL==fbmp) {
			printf("%s open if failed\n",argv[1]);
			return 0;
		}
		fout=fopen(argv[2],"wb");
		if(NULL==fout) {
			printf("%s open of failed\n",argv[2]);
			return 0;
		}
	} else {
		printf("no file arg\n");
		return 0;
	}
	
	memset(&bmp_h,0,sizeof(bmp_file_header));
	memset(&bmp_i,0,sizeof(bmp_info_header));
	fread(&bmp_h,sizeof(bmp_file_header),1,fbmp);
	fread(&bmp_i,sizeof(bmp_info_header),1,fbmp);
	
	bmp_i.xppm=0x000000bb;
	bmp_i.yppm=0x00000000;
	bmp_i.size=0x28;
	
	fwrite(&bmp_h,sizeof(bmp_file_header),1,fout);
	fwrite(&bmp_i,sizeof(bmp_info_header),1,fout);
	
	size_t useless_cnt=bmp_i.size-sizeof(bmp_info_header);
	unsigned char useless_char=0;
	while(useless_cnt>0) {
		fread(&useless_char,sizeof(unsigned char),1,fbmp);
		--useless_cnt;
	}

	uint8_t rest;
	size_t rest_i;
	while(1) {
		rest_i=fread(&rest,sizeof(uint8_t),1,fbmp);
		if(rest_i>0) {
			fwrite(&rest,sizeof(uint8_t),1,fout);
		} else {
			printf("%s copied\n",argv[2]);
			break;
		}
	}
	
	fclose(fbmp);
	fclose(fout);
}